package com.lzb.party.controller;

import com.lzb.party.entity.Notice;
import com.lzb.party.mapper.NoticeMapper;
import com.lzb.party.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired(required=false)
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeService noticeService;

    @PostMapping
    //新增或更新
    private boolean save(@RequestBody Notice notice){
        //@RequestBody注释把前台的Json数据变成User对象
        return noticeService.saveNotice(notice);
    }

    @GetMapping()
    private List<Notice> findAll(){
        return noticeMapper.findAll();
    }

    @DeleteMapping("/{num}")
    private Integer delete(@PathVariable Integer num){
        return noticeMapper.deleteById(num);
    }

    @GetMapping("/page")
    private Map<String, Object> findPage(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize,
                                         @RequestParam String name,
                                         @RequestParam String num){
        pageNum = (pageNum-1)*pageSize;
        name = "%"+name+"%";
        num = "%"+num+"%";
        List<Notice> data = noticeMapper.selectPage(pageNum,pageSize,name,num);
        Integer total = noticeMapper.selectTotal(name,num);
        Map<String, Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }
}
